package Offer;

/**
 * 树的子结构
 * @author 是阿秋啊
 * @date 2022/04/05 21:40
 **/
public class Num26 {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (A == null || B == null) {
            return false;
        }
        return isSameTree(A,B) || isSubStructure(A.left,B) || isSubStructure(A.right,B);
    }

    private boolean isSameTree(TreeNode a, TreeNode b) {
        if (a == null && b == null){
            return true;
        }
        if (a == null) {
            return false;
        }
        if (b == null) {
            return false;
        }
        return a.val == b.val && isSameTree(a.left,b.left) && isSameTree(a.right,b.right);
    }
}
